##HarbridgeYong_Bipartisanship_Staff_Replication.R
##Harbridge-Yong, Laurel Congressional Capacity Chapter replication files for analysis of bipartisanship
## by staff 
##Data come from Congressional Capacity Staff Survey

##Load data
##set working directory
setwd("C:/Box Sync/My Documents/Congressional capacity scholars group/Replication Files")
setwd("C:/Users/lmh735/Dropbox (Political Science)/Cong'l Capacity Survey 2017 Data") ##Data from Drutman et al.
data<- read.csv("CCS_2017_1.3_labels.csv", header=TRUE) ##updated data to 1.3 with labels
dim(data)
names(data)

##working directory for all output
setwd("C:/Box Sync/My Documents/Congressional capacity scholars group/Replication Files")

###########################################
##   Data preparation and re-coding      ##
###########################################
##Recode variables
##oficetype2
summary(as.factor(data$officetype2))

##legislative focused or PR focused
##alternative is "title_cat" variable
data$leg.focused<- ifelse(data$q98_1=="Chief of Staff" | data$q98_2=="Chief of Staff" | data$q98_3=="Chief of Staff" |
                            data$q98_1=="Leg Director" | data$q98_2=="Leg Director" | data$q98_3=="Leg Director" |
                            data$q98_1=="Deputy Cos" | data$q98_2=="Deputy Cos" | data$q98_3=="Deputy Cos" | 
                            data$q98_1=="Leg Counsel" | data$q98_2=="Leg Counsel" | data$q98_3=="Leg Counsel" |
                            data$q98_1=="Prof Staff Member" | data$q98_2=="Prof Staff Member" |data$q98_3=="Prof Staff Member" |
                            data$q98_1=="Subc SD" | data$q98_2=="Subc SD" | data$q98_3=="Subc SD" |
                            ##data$q98_1=="Staff Asst" | data$q98_2=="Staff Asst" | data$q98_3=="Staff Asst" |
                            data$q98_1=="Leg Asst" | data$q98_2=="Leg Asst" | data$q98_3=="Leg Asst" |
                            data$q98_1=="Leg Correspondent" | data$q98_2=="Leg Correspondent" | data$q98_3=="Leg Correspondent" | 
                            ##data$q98_1=="Fellow" |data$q98_2=="Fellow" | data$q98_3=="Fellow" |
                            data$q98_1=="LC, inclus" | data$q98_2=="LC, inclus" | data$q98_3=="LC, inclus", 1, 0)
summary(as.factor(data$leg.focused))                          


data$pr.focused<- ifelse(data$q98_1=="Press Secretary" | data$q98_2=="Press Secretary" | data$q98_3=="Press Secretary" |
                           data$q98_1=="Comms Director" | data$q98_2=="Comms Director" | data$q98_3=="Comms Director" |
                           data$q98_1=="House Press Secretary" | data$q98_2=="House Press Secretary" | data$q98_3=="House Press Secretary", 1, 0) 
summary(as.factor(data$pr.focused))


##Bipartisanship
##communication with staffers in your own party
data$comm.own<- ifelse(data$bipart_comm_1=="Never", 0,
                       ifelse(data$bipart_comm_1=="Rarely", 1,
                              ifelse(data$bipart_comm_1=="Sometimes", 2,
                                     ifelse(data$bipart_comm_1=="Often", 3,
                                            ifelse(data$bipart_comm_1=="Always", 4, NA)))))
summary(as.factor(data$comm.own))

##Communication with staffers in the opposing party
data$comm.other<- ifelse(data$bipart_comm_2=="Never", 0,
                       ifelse(data$bipart_comm_2=="Rarely", 1,
                              ifelse(data$bipart_comm_2=="Sometimes", 2,
                                     ifelse(data$bipart_comm_2=="Often", 3,
                                            ifelse(data$bipart_comm_2=="Always", 4, NA)))))
summary(as.factor(data$comm.other))

##When working to advance legislation, which do you focus on when communicating with own/other
##recode for focus on policy content (include voting coalition and conveying policy positions to outside groups together)
##own
summary(data$bipart_comm_topic_1)
96/(96+208+15)
208/(96+208+15)
15/(96+208+15)
data$comm.own.policy<- ifelse(data$bipart_comm_topic_1=="Content", 1, 
                              ifelse(data$bipart_comm_topic_1=="Coalition" | data$bipart_comm_topic_1=="Outside", 0, NA))
summary(as.factor(data$comm.own.policy)) ##correct that 122 NA if no answers to these (so shouldn't be lumped with 0s)
data$comm.own.coalition<- ifelse(data$bipart_comm_topic_1=="Coalition", 1, 
                                 ifelse(data$bipart_comm_topic_1=="Content" | data$bipart_comm_topic_1=="Outside", 0, NA))
summary(as.factor(data$comm.own.coalition)) 
data$comm.own.outside<- ifelse(data$bipart_comm_topic_1=="Outside", 1, 
                                 ifelse(data$bipart_comm_topic_1=="Content" | data$bipart_comm_topic_1=="Coalition", 0, NA))
summary(as.factor(data$comm.own.outside)) 

##other
summary(data$bipart_comm_topic_2) ##correct that 129 NA
93/(93+201+18)
201/(93+201+18)
18/(93+201+18)
data$comm.other.policy<- ifelse(data$bipart_comm_topic_2=="Content", 1, 
                                ifelse(data$bipart_comm_topic_2=="Coalition" | data$bipart_comm_topic_2=="Outside", 0, NA))
summary(as.factor(data$comm.other.policy))
data$comm.other.coalition<- ifelse(data$bipart_comm_topic_2=="Coalition", 1, 
                                 ifelse(data$bipart_comm_topic_2=="Content" | data$bipart_comm_topic_2=="Outside", 0, NA))
summary(as.factor(data$comm.other.coalition)) 
data$comm.other.outside<- ifelse(data$bipart_comm_topic_2=="Outside", 1, 
                               ifelse(data$bipart_comm_topic_2=="Content" | data$bipart_comm_topic_2=="Coalition", 0, NA))
summary(as.factor(data$comm.other.outside)) 


##Independent variables:
##tenure - total tenure in any congressional office

##how long do you envision working within Congress
data$expect.cong<- ifelse(data$q29=="Within 12 mos", 1,
                          ifelse(data$q29=="1-2 years", 2,
                                 ifelse(data$q29=="3-5 years", 3,
                                        ifelse(data$q29=="6-10 years", 4,
                                               ifelse(data$q29=="More 10 years", 5, NA)))))
summary(as.factor(data$expect.cong))
##categorical variable
data$expect.cong.category<- data$q29
##Make within 12 mos the reference category
data$expect.cong.category.new<- relevel(data$expect.cong.category, ref="Within 12 mos")

##chamber
data$senate<- ifelse(data$chamber=="Senate", 1, 0) ##0 is House and 5 people who are other
summary(as.factor(data$senate))

##Ideology
summary(data$ideo_centered)
##Ideological extremity
data$ideo_extremity<- abs(data$ideo_centered)
summary(data$ideo_extremity)

##Party
summary(data$party_ls2)

##Gender (female, male)
summary(data$female)

#########################################
##     Set up survey weights           ##
#########################################
##psweight_g is post-strateification weights on chamber, party, and gender

##check weights
range(data$psweight_g, na.rm=TRUE)
mean(data$psweight_g, na.rm=TRUE)
summary(data$psweight_g)

##more weights checks
table(data$female) / length(data$female)
table(data$female) / na.omit(length(data$female))

library(survey)
data.weighted<- svydesign(id=~ccsid, weights=~psweight_g, data=data)

##check weights
svymean(~female, data.weighted)
tapply(data$psweight_g, data$female, sum) / sum(data$psweight_g)


##########################################
##        Analysis for chapter          ##
##########################################

##means of communication for own/other
svymean(~ comm.own, design=data.weighted, na.rm=TRUE)
svymean(~ comm.other, design=data.weighted, na.rm=TRUE)
##svyttest(~ comm.own + comm.other, design=data.weighted, na.rm=TRUE)
##with just ttest
t.test(data$comm.own, data$comm.other, na.rm=T)

###################################
##Regression results for Table 14.1 (communication with staff in the other party)

##Model 1 
comm.other.model1<- svyglm(comm.other ~ officetype2 + pr.focused + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, design=data.weighted)
summary(comm.other.model1)

##Model 2
comm.other.model2<- svyglm(comm.other ~ officetype2 + pr.focused + comm.own + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, design=data.weighted)
summary(comm.other.model2)

##Model 3
comm.other.model3<- svyglm(comm.other ~ officetype2 + pr.focused + comm.own + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity + expect.cong.category.new, design=data.weighted)
summary(comm.other.model3) 

##Model 4
##DV difference between own/other (higher values = more communication with own side)
comm.other.model4<- svyglm(I(comm.own-comm.other) ~ officetype2 + pr.focused + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity + expect.cong.category.new, design=data.weighted)
summary(comm.other.model4) 

##Robustness check
##check robust to ordered logit (not using survey weights)
library(MASS)
comm.other.model1.ol<- polr(as.factor(comm.other) ~ officetype2 + pr.focused + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, data=data, Hess=TRUE)
summary(comm.other.model1.ol)

comm.other.model2.ol<- polr(as.factor(comm.other) ~ officetype2 + pr.focused + comm.own + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, data=data, Hess=TRUE)
summary(comm.other.model2.ol)

comm.other.model3.ol<- polr(as.factor(comm.other) ~ officetype2 + pr.focused + comm.own + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity + expect.cong.category.new, data=data, Hess=TRUE)
summary(comm.other.model3.ol)


##Output regressions
source("regtable.R") ##Courtesy of Alex Tahk

outtable.rtf(list("(1) Other"=comm.other.model1, "(2) Other"=comm.other.model2, "(3) Other" = comm.other.model3, "(4) Own-Other"=comm.other.model4),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("officetype2Party/Adm", "Party/Adm Office"),
                              c("officetype2Personal", "Personal Office"),
                              c("pr.focused", "PR Focused"),
                              c("comm.own", "Frequency of communication with own party staff"),
                              c("tenure", "Tenure"),
                              c("I(tenure^2)", "Tenure Squared"),
                              c("party_ls2Republican", "Republican"),
                              c("femaleMale", "Male"),
                              c("expect.cong", "Expected time working in Congress"),
                              c("senate", "Senate"),
                              c("ideo_extremity", "Ideological Extremity"),
                              c("expect.cong.category.new1-2 years", "Expected time in Congress 1-2 yrs"),
                              c("expect.cong.category.new3-5 years", "Expected time in Congress 3-5 yrs"),
                              c("expect.cong.category.new6-10 years", "Expected time in Congress 6-10 yrs"),
                              c("expect.cong.category.newMore 10 years", "Expected time in Congress 10+ yrs")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 2,
             p.levels.labels=c("^", "*","**","***"),
             "HarbridgeYong_14_table001.rtf")

##interpretation of tenure variable (model 2)
##create dummy variables for each categorical
data$officetype2Party<- ifelse(data$officetype2=="Party/Adm", 1, 0)
data$officetype2Personal<- ifelse(data$officetype2=="Personal", 1, 0)
data$party_ls2Republican<- ifelse(data$party_ls2=="Republican", 1, 0)
data$femaleMale<- ifelse(data$female=="Male", 1, 0)
data.weighted<- svydesign(id=~ccsid, weights=~psweight_g, data=data)
comm.other.model2.test<- svyglm(comm.other ~ officetype2Party + officetype2Personal + pr.focused + comm.own + tenure + I(tenure^2) + party_ls2Republican + femaleMale + senate + ideo_extremity, design=data.weighted)
summary(comm.other.model2.test)
pred.tenure<- predict(comm.other.model2.test, newdata=expand.grid(list(officetype2Party=0,
                                                                 officetype2Personal=1,
                                                            pr.focused=median(data$pr.focused, na.rm=TRUE),
                                                            comm.own=mean(data$comm.own, na.rm=TRUE),
                                                            tenure=seq(1,18, by=1),
                                                            party_ls2Republican=1,
                                                            femaleMale=1,
                                                            senate=median(data$senate, na.rm=TRUE),
                                                            ideo_extremity=mean(data$ideo_extremity, na.rm=TRUE))),
                              se.fit=TRUE)
pred.tenure
pred.tenure<- data.frame(pred.tenure)
names(pred.tenure)

tenure<- seq(1,18,by=1)
plot(pred.tenure$link ~ tenure, type="l", main="Effect of Tenure on Communication with Other Party",
     ylim=c(0,4)) ##tenure has a positive effect until year 11 and then decreases
polygon(c(tenure, rev(tenure)),
        c((pred.tenure$link + qnorm(.05/2,lower.tail=F)*pred.tenure$SE),
          rev(pred.tenure$link - qnorm(.05/2,lower.tail=F)*pred.tenure$SE)),
        col="grey91",
        border=NA,
        add=T)
lines(pred.tenure$link ~ tenure,
      type="l",
      lty=1)
max(pred.tenure$link) ##in year 11

##Analyses underlying foonote on predictors of communication focused on policy content and frequency of communication
##When you work to advance legislation and communicate with ____, do you focus most on policy content?
##binary so use logistic

##Staffers from own party
comm.own.policy2<- svyglm(comm.own.policy ~ officetype2 + pr.focused + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, design=data.weighted, family=binomial(link="logit"))
summary(comm.own.policy2)

##Staffers from opposing party
comm.other.policy2<- svyglm(comm.other.policy ~ officetype2 + pr.focused + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, design=data.weighted, family=binomial(link="logit"))
summary(comm.other.policy2)

##frequency of policy versus other communication
##means on policy via svy weights (reported in chapter) and raw
##content of communication with own party staff
svymean(~comm.own.policy, design = data.weighted, na.rm=TRUE)
summary(data$comm.own.policy, na.rm=TRUE)
svymean(~comm.own.coalition, design = data.weighted, na.rm=TRUE)
summary(data$comm.own.coalition, na.rm=TRUE)
svymean(~comm.own.outside, design = data.weighted, na.rm=TRUE)
summary(data$comm.own.outside, na.rm=TRUE)

##content of communication with opposing party staff
svymean(~comm.other.policy, design = data.weighted, na.rm=TRUE)
summary(data$comm.other.policy, na.rm=TRUE)
svymean(~comm.other.coalition, design = data.weighted, na.rm=TRUE)
summary(data$comm.other.coalition, na.rm=TRUE)
svymean(~comm.other.outside, design = data.weighted, na.rm=TRUE)
summary(data$comm.other.outside, na.rm=TRUE)



#####################################################################
##  Robustness check of main models excluding PR focused members   ##
#####################################################################
##drop own communication
r.comm.other.model1<- svyglm(comm.other ~ officetype2 + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, design=data.weighted, subset=c(data$pr.focused==0))
summary(r.comm.other.model1)

##control for tenure and party
r.comm.other.model2<- svyglm(comm.other ~ officetype2 + comm.own + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity, design=data.weighted, subset=c(data$pr.focused==0))
summary(r.comm.other.model2)

##add control for how long you envision working in Congress
r.comm.other.model3<- svyglm(comm.other ~ officetype2  + comm.own + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity + expect.cong.category.new, design=data.weighted, subset=c(data$pr.focused==0))
summary(r.comm.other.model3) 

##DV difference between own/other (higher values = more communication with own side)
r.comm.other.model4<- svyglm(I(comm.own-comm.other) ~ officetype2 + tenure + I(tenure^2) + party_ls2 + female + senate + ideo_extremity + expect.cong.category.new, design=data.weighted, subset=c(data$pr.focused==0))
summary(r.comm.other.model4) 



